package com.library.service.admin;

import com.library.dao.AdminDao;
import com.library.entity.Admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class AdminService {

    private final AdminDao adminDao = new AdminDao();

    /**
     * 管理员登录
     *
     * @param username
     * @param password
     * @param request
     * @return
     */
    public boolean login(String username, String password, HttpServletRequest request) {
        if (username == null || username.trim().isEmpty() || password == null || password.trim().isEmpty()) {
            // 0.用户名或密码为空
            request.setAttribute("errorMsg", "用户名或密码不能为空");
            return false;
        }
        // 1.查询管理员信息
        Admin admin = adminDao.getByUsername(username);
        if (admin == null || !admin.getPassword().equals(password)) {
            // 2.用户名错误或密码错误
            request.setAttribute("errorMsg", "用户名或密码错误");
            return false;
        }
        // 3.管理员信息校验成功
        HttpSession session = request.getSession();
        session.setAttribute("admin", admin);
        return true;
    }

}
